#include "PhanCung.h"
#include "GiaoDich.h"
#include "TaiKhoan.h"
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
using namespace std;
BanPhim::BanPhim()
{
}
int BanPhim::PhimNhan()
{
	int temp;
	do
	{
		temp = getch();
	}
	while((temp !=13)&&(temp !=27)&&(temp!=127)&&((temp<48)||(temp >57)));
	phimNhan = temp;
	return phimNhan;
}
ManHinh::ManHinh()
{
	mhTrangThai=0;
}
bool ManHinh::Confirm(int type)
{
	BanPhim bp;
	int temp;
	switch(type){
		case 1:
			system("cls");
			cout<<"\nBan co chac chan muon huy giao dich";
			cout<<"\nPress 1 to agree";
			cout<<"\nPress 0 to cancel";
			break;
		case 2:
			system("cls");
			cout<<"\nBan co muon thuc hien giao dich khac khong?";
			cout<<"\nPress 1 to agree";
			cout<<"\nPress 0 to cancel";
		default:
			break;
	}
	do
	{
		temp = bp.PhimNhan();
	}
	while (temp!=48 && temp!=49);
	if (temp==48)
		return false;
	else
		return true;
}
int ManHinh::HienThi(int trangThai)
{
	mhTrangThai = trangThai;
	KheDocThe kheDocThe;
	BanPhim bp;
	TaiKhoan tk;
	int phimNhan, temp, tiepTuc;
	while (1)
	{
		system("cls");
	    Welcome();
		do
		{
			phimNhan = bp.PhimNhan();
		}
		while(phimNhan != 13);
		trangThai  = kheDocThe.KiemTraMaPin();
		if (trangThai ==2)
		{
			ThongBao(2);
			trangThai=0;
		}
		else if (trangThai==31)
		{
			tiepTuc=0;
			ThongBao(31);
			trangThai=1;
			
			//thuc hien giao dich
			//code main
			do
			{
				MenuChinh();//in menu chinh
				
				switch(bp.PhimNhan())
				{
					case 27://ESC
						if (Confirm(2))
							tiepTuc=0;
						break;
					case 49://1. rut tien
						ThongBao(5);
						temp = tk.RutTien();
						if (temp ==0)
						{
							tiepTuc = 1;
							break;
						}
						tiepTuc = 0;
						ThongBao(temp);//thong bao giao dich thanh cong
						break;
					case 50://2. In sao ke
					case 51://3. Xem so du
					case 52://4. Chuyen khoan
					default://"0-2-4-6-9"~~""48-50-52-54-57"
						break;
				}
				//getch();//for debug				
			}
			while(tiepTuc==1);
		}
		else 
		{
			ThongBao(32);
			trangThai=0;
		}
	}
	return mhTrangThai;
}
void ManHinh::MenuChinh()
{
	system("cls");
	TaoMau();
    cout << "\n";
	cout << " |--------------- * -------------- * ----------------------|\n";
	cout << " |---------------------------------------------------------|\n";
	cout << " ||                                                       ||\n";
	cout << " ||                                                       ||\n";
	cout << " ||                                                       ||\n";
	cout << " ||                                                       ||\n";
    cout << " ||               CAC DICH VU CUA NGAN HANG ABC:          ||\n";
	cout << " ||                    1. RUT TIEN                        ||\n";
	cout << " ||                    2.IN SAO KE                        ||\n";
	cout << " ||                    3.XEM SO DU TAI KHOAN              ||\n";                                                                                                                                                                  
    cout << " ||                    4.CHUYEN KHOAN                     ||\n";
    cout << " ||                                                       ||\n";
    cout << " ||                                                       ||\n";
    cout << " ||                                                       ||\n";
	cout << " |------------------------------------------  --------------\n";
    cout << " || NEU MUON HUY GIAO DICH QUY KHACH HAY NHAN PHIM CANCEL ||\n";
    cout << " |--------------- * --------------- * ---------------------|\n";	
     
} 
void ManHinh::TaoMau()
{
   system("color 2");     
}
void ManHinh::Welcome()
{
    TaoMau();
    cout << "\n";
	cout << " |--------------- * -------------- * -------------- * --------------|\n";
	cout << " |------------------------------------------------------------------|\n";
	cout << " ||                                                                ||\n";
	cout << " ||                                                                ||\n";
	cout << " ||                                                                ||\n";
	cout << " ||                                                                ||\n";
    cout << " ||                      WELLCOME TO ABC BANK!!!                   ||\n";
	cout << " ||                          NGAN HANG ABC                         ||\n";
	cout << " ||                      HAN HANH DUOC PHUC VU                     ||\n";
	cout << " ||                            QUY KHACH!                          ||\n";                                                                                                                                                                  
    cout << " ||                                                                ||\n";
    cout << " ||                                                                ||\n";
    cout << " ||                                                                ||\n";
	cout << " |------------------------------------------------------------------\n";
    cout << " ||          QUY KHACH HAY NHAN PHIM ENTER DE CHO THE VAO!         ||\n";   
    cout << " |--------------- * --------------- * ------------- * --------------|\n";
         
}

void ManHinh::ThongBao(int type)
{
	TaiKhoan tk;
	switch(type)
	{
		case 0: 
			break;
		case 1:
			break;
		case 2:
			system("cls");
			cout<<"\nThe cua ban bi tu choi";     
			cout<<"\nVui long lien he voi nhan vien de duoc nhan lai the";  
			Sleep(1500);
		case 3: //NhapMaPin()
			cout<<"\nMoi ban nhap ma PIN:";     
			break;
		case 4: //NhapLaiMaPin() 
			cout<<"\nBan da nhap sai ma PIN";     
			cout<<"\nMoi ban nhap lai ma PIN:"; 
			break;
		case 5://RutTien
			system("cls");
			cout<<"\n   "<<tk.TenTK();
			cout<<"\n   "<<tk.SoTK();
			cout<<"\nMoi ban chon so tien muon rut\n";
			cout<<"\n1. $50";
			cout<<"\n2. $200 ";
			cout<<"\n3. $500 ";
			cout<<"\n4. $1000 ";
			cout<<"\n5. $5000 ";
			cout<<"\n6. So khac";
			break;
		case 6://khi nhap ma pin
			system("cls");
			cout<<"\nNhan ENTER de thuc hien ";     
			cout<<"\nNhan BACKSPACE de xoa ky tu vua nhap";     
			cout<<"\nNhan ESC neu ban muon huy bo giao dich ";     
			break;
		case 7:
			system("cls");
			cout<<"\nNhan ENTER de thuc hien ";     
			cout<<"\nNhan BACKSPACE de xoa ky tu vua nhap";     
			cout<<"\nNhan ESC neu ban muon huy bo giao dich ";     
			cout<<"\n\n";
			cout<<"\nNhap so tien can rut:";     
			break;
		case 10:
			system("cls");
			cout<<"\nSo tien trong tai khoan cua ban ko du de thuc hien giao dich";     
			break;
		case 20:
			system("cls");
			cout<<"\nMay ATM het tien";     
			break;
		case 21:
			system("cls");
			cout<<"\nCo loi trong qua trinh rut tien";
			break;
		case 30:
			system("cls");
			cout<<"\nGiao dich thanh cong";
			cout<<"\nMoi ban nhan rut the va nhan lai tien";
			cout<<"\nXin cam on";
			Sleep(2000);
			break;
		case 31: 
			system("cls");
			cout<<"\nBan da dang nhap thanh cong";     
			Sleep(1000);
			break;
		case 32:
			system("cls");
			cout<<"\nBan da huy giao dich thanh cong";     
			Sleep(1000);
			break;
		default:
			break;
	}
}

void ManHinh::inSao()
{
	cout<<"*";
}
void ManHinh::inChar(int kyTu)
{
	cout<<char(kyTu);
}
KheDocThe::KheDocThe()
{
}
//KheDocThe::KiemTraMaPin()
//0 neu muon thoat chuong trinh
//1 dang nhap OK
//2 dang nhap sai
int KheDocThe::KiemTraMaPin()
{
	TaiKhoan tk;
	ManHinh mh;
	BanPhim bp;
	tk.DocThe();
	int soLanNhap = 0;
	int maPin=0;
	int phimNhan,kyTuNhap=0;
	int trangThai=2; //dang nhap ko thanh cong
	do
	{
		mh.ThongBao(6);
		cout<<"\n\n\nChao ban "<<tk.TenTK();
		if (soLanNhap==0)
			mh.ThongBao(3);
		else
			mh.ThongBao(4);
		kyTuNhap = 0;
		maPin = 0;
		do
		{
			phimNhan = bp.PhimNhan();
			//neu nhap nhieu hon 6 ky tu thi ko cho nhap nua 
			//chi cho phep xoa di nhap lai
			//hoac chon ENTER de thuc hien so sanh
			//hoac ESC de thoat
			if (phimNhan==13 && kyTuNhap==6)//thuc hien so sanh
			{
				break;
			}
			if (phimNhan==13 || kyTuNhap>=6)//thuc hien so sanh
				continue;
			if (phimNhan==127)//xoa
			{
				kyTuNhap =0;
				maPin = 0;
				continue;
			}
			if (phimNhan==27)//thoat
			{
				return 0;
			}
			mh.inSao();
			maPin = maPin*10+(phimNhan-48); //phimNhan-48 la gia tri cua cac phim so 
			kyTuNhap++;
		} while(kyTuNhap!=13);
		if(maPin==tk.MaPin())
		{
			return 31;//dang nhap thanh cong;
		}
		else
		{
			soLanNhap++;//dang nhap sai
			trangThai = 2;
		}
	}
	while (soLanNhap<3);
	return trangThai;
}
HopDungTienMat::HopDungTienMat()
{
	loaiTien[0].soLuong = 100;//100 to
	loaiTien[0].menhGia = 500;//500 ngan
	loaiTien[1].soLuong = 100;
	loaiTien[1].menhGia = 200;
	loaiTien[2].soLuong = 100;
	loaiTien[2].menhGia = 100;
	loaiTien[3].soLuong = 100;
	loaiTien[3].menhGia = 50;
	loaiTien[4].soLuong = 100;
	loaiTien[4].menhGia = 20;
	loaiTien[5].soLuong = 100;
	loaiTien[5].menhGia = 10;
	tongTien = 0;
	for (int i = 0; i<6; i ++)
	{
		tongTien+=loaiTien[i].soLuong*loaiTien[i].menhGia;
	}
};
LoaiTien *HopDungTienMat::Tien()
{
	return loaiTien;	
}
long int HopDungTienMat::TongTien()
{
	return tongTien;
}
int HopDungTienMat::TraTien(int tienCanRut)
{
	LoaiTien loaiTienTra[6];
	//GiaoDich gd;
	int soLuong, temp = tienCanRut;	
	int i=0;
	while(i<6)
	{
		loaiTienTra[i].soLuong = 0;
		loaiTienTra[i].menhGia = loaiTien[i].menhGia;
		if (loaiTienTra[i].menhGia<=temp)
		{
			soLuong = temp/loaiTien[i].menhGia;
			if(soLuong>=loaiTien[i].soLuong)
			{
				loaiTienTra[i].soLuong = loaiTien[i].soLuong;
				loaiTien[i].soLuong = 0;
				temp=temp-loaiTienTra[i].soLuong*loaiTienTra[i].menhGia;
			}
			else
			{
				loaiTienTra[i].soLuong = soLuong;
				loaiTien[i].soLuong = loaiTien[i].soLuong - soLuong;
				temp=temp-loaiTienTra[i].soLuong*loaiTienTra[i].menhGia;
			}
			if (temp ==0)
				break;
		}
		i++;
	}
	//cout<<"\n tien can rut"<<temp;
	//getch();//for debug
	if (temp)
		return 20;//ATM thieu tien
	if (temp<0)
		return 21;//loi khi rut tien
	//update GiaoDich
	//gd.AddGiaoDich(4,tienCanRut);
	gGiaoDich.AddGiaoDich(4,tienCanRut);
	return 30;//giao dich thanh cong
}
